//
//  TicTacController.m
//  TicTacToe
//
//  Created by Tim on 09-11-23.
//  Copyright 2009 Tim Dimsdale. All rights reserved.
//
// This file is part of TicTacToe.
// 
// TicTacToe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// TicTacToe is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with TicTacToe.  If not, see <http://www.gnu.org/licenses/>
//
#import "TicTacController.h"


@implementation TicTacController


// constructor
-(id) init {
	self = [super init];
	if ( self ) {
		myModel = [[TicTacModel alloc]init];
		opponent = [[TicTacOpponent alloc]init];

		allowMoves = false;
		// \todo make this configurable.
		computerPlayer = TICTAC_X;
		humanPlayer = TICTAC_O;
		
		[opponent setPlayerCode:computerPlayer];
		[opponent setModel: myModel];
		
		[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(humanEventNotificationHandler:) name:HumanPerformedEventNotification object:nil];
	}
	return self;
}// init

-(void) dealloc
{
	[myModel release];
	[opponent release];
	[super dealloc];
}

- (TicTacModel*)getModel {
	return myModel;
}

- (IBAction)newGame:(id)pId 
{
	[myModel clear];
	[myView setModel:myModel];
	[myView newGame]; 
	allowMoves = true;

	[opponent setSkillLevel:[currentComputerSkill intValue]];
	
	QTMovie* soundToPlay = [[QTMovie alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"maybe win maybe wont.m4a" ofType:nil] error:nil];
	[soundToPlay play];
	
}

- (IBAction)changeComputerSkillLevel:(id)pId 
{
//	NSLog(@"slider");
	[opponent setSkillLevel:[currentComputerSkill intValue]];
//	[opponent setSkillLevel:[currentSkillValue ] ];
	
}

/*-(void) tictacViewLoadedNotificationHandler:(NSNotification *)notification 
{
	// so, we need to do this after the view has been attached
	[self newGame:nil];	
}*/

- (void)humanEventNotificationHandler:(NSNotification *)notification {
	//NSLog(@"got notification !!");
	TicTacPosition* humanDesiredPosition = [notification object];
	[self takeTurns:humanDesiredPosition];
}

- (void)takeTurns:(TicTacPosition* ) humanDesiredPosition {
	if ( !allowMoves )
		return;
	
	bool didHumanMove = [self humanTakeTurn:humanDesiredPosition];
	int winner = [myModel getAnyWinner];
	
	if ( didHumanMove ) {
		if ( humanPlayer != winner ) 
		{
			[self computerTakeTurn];
			winner =  [myModel getAnyWinner];
		}
	}
	
	[myView updateDisplay];
	
	//[self setNeedsDisplay: YES];
	
	if ( computerPlayer == winner ) 
	{
		allowMoves = false;
		QTMovie* soundToPlay = [[QTMovie alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"oh no.m4a" ofType:nil] error:nil];
		[soundToPlay play];		
	} else if ( humanPlayer == winner ) {
		allowMoves = false;
		QTMovie* soundToPlay = [[QTMovie alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"Hooray.m4a" ofType:nil] error:nil];
		[soundToPlay play];
	}	
	[myView setIsActive:allowMoves];
	
	
}// take turns




- (bool)humanTakeTurn:(TicTacPosition *)humanDesiredPosition {
	bool didHumanMove = true;
	
	NSError *humanError = nil;
	[myModel setPlayerAtX:[humanDesiredPosition getX] andY:[humanDesiredPosition getY] withPlayer: humanPlayer error:&humanError ];
	if ( nil != humanError ) {
		didHumanMove = false;
		if ( [humanError code] == ERROR_PLAYING_SPACE_NOT_AVAILABLE ) {
			QTMovie* soundToPlay = [[QTMovie alloc] initWithFile:[[NSBundle mainBundle] pathForResource:@"uh oh.m4a" ofType:nil] error:nil];
			[soundToPlay play];
			
		} else {
			// an unexpected error... just display it for now.
			NSAlert *theAlert = [NSAlert alertWithError:humanError];
			[theAlert runModal]; 
			
		}
	}
	return didHumanMove;

}

-(void) computerTakeTurn
{
	NSError *computerError = nil;
	[opponent takeTurn: &computerError];
	if ( nil != computerError ) 
	{
		if ( [computerError code] == ERROR_NO_MOVE_POSSIBLE )
		{
			// \todo play 'doh' music	
		} else {
			// an unexpected error... just display it for now.
			NSAlert *theAlert = [NSAlert alertWithError:computerError];
			[theAlert runModal]; 
		}
		
	}
	
}


@end
